<?php
/**
 * Secure INCLUDER
 *
 * @version 0.2a
 */

// No direct access
defined( "__DEENZO__" ) or die( "deenzo$" );

/* Include a script */
function sh( $sh, $args = "" ) {

    /* Parse ARGS */
	eval( ARGL );
	
	// Support for non PHP-extension scripts
    $sh = preg_replace( "/\.(" . __PHP__  . ")$/", "", $sh );
    
    /* Support for directory and prefix discover */
    if ( $d || $p ) {
        $dir  = ( !$d ) ?  dirname( $sh ) : $sh;
        $pre  = (  $p ) ? basename( $sh ) : "" ;
        
        if ( $f && !is_dir( $dir ) )
            return false;
        
        $argz = preg_replace( "/(d|p)/", "", $args );
        
        foreach ( scandir( $dir ) as $e ) {
            
            // Ignore backup and system files
            if ( preg_match( "/^(?!\.|~)/", $e ) ) {
                
                if ( $R and is_dir( "$dir/$e" ) )
                   load_php( "$dir/$e/$pre" , $args );
                
                elseif ( is_file( "$dir/$e" ) ) {
                    if ( $d ) 
                        load_php( "$dir/$e" , $argz );
                    
                    elseif ( $p and preg_match( "/^$pre/", $e ) )
                        load_php( "$dir/$e" , $argz );
                }
            } 
        }
        
    } else {

        /** REQUIRE is default include method */
        $mode  = ( $i ) ? "include" : "require";
        $mode .= ( $o ) ? "_once"   : "";
        
        if ( $f && !file_exists( "$sh." . __PHP__ ) )
            return false;
        
        if ( $e )
            return true;
            
        eval( "\$r = $mode( '$sh.' . __PHP__ );" );
        
        return ( $l ) ? $sh : $r;
    }

}

/* Load a CHROME shell script */
function chrome( $sh, $args = "-of" ) {

    /* Default location on CHROME */
	if ( !sh( __DIR__ . "/chrome/$sh", $args ) ) {
		
		$cll = debug_backtrace(); $cll = $cll[0]["file"] . ":" . $cll[1]["file"];
		
		/* Detects callback, if USR or SPOOL, search inside USR directory */
		if ( preg_match( "/\/(usr|spool)\//", $cll ) )
	        return sh( __DIR__ . "/chrome/usr/$sh", $args );
		else
			return false;
	
	} else
		return true;

}

function bin( $bin ) {
	return chrome( "bin/$bin", "-of" );
}

function coreutil( $coreutil ) {
	return chrome( "coreutils/$coreutil" );
}

function etc( $etc ) {
	return chrome( "etc/$etc" );
}

function lib( $lib ) {
	return chrome( "lib/$lib" );
}

function sbin( $sbin ) {
	return chrome( "sbin/$sbin" );
}

function usr( $usr ) {
    return chrome( "usr/$usr" );
}

function X11( $X11 = "" ) {

    if ( $X11 ) {
        if ( etc( "X11/$X11" ) ) return true;
    	
    	if ( defined("__THEME_DIR__") )
    	    return sh( __THEME_DIR__ . "/$X11", "-of" );
    	
    	return false;
    	   
    } else {
    
        /** Prevents use of blank X11 param */
        if ( !env("flags.X11.locked") ) {
            
            env( "flags.X11.locked", true );
            
            $X11 = preg_split( "/\./", __X11__ );
            
            /* Single X11 window reference */
            if ( count( $X11 ) == 1 )
                return X11( $X11[0] );
            else {
                
                /* X11 window as foo/bar */
                if ( X11( join( "/", $X11 ) ) ) 
                    return true;
                
                /* X11 window as foo.bar */
                elseif ( X11( join( ".", $X11 ) ) )
                    return true;
                
                /* X11 window as bar or foo */
                else {
                
                    for ( $i = count( $X11 ); $i > -1; $i-- )
                        if ( X11( $X11[ $i ] ) )
                            return true;
                
                    return false;
                }
            }
        }
    }
}



